﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RapChieuPhim.DTO;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

namespace RapChieuPhim.DAO
{
    class CaChieuDAO
    {
        public CaChieuDTO GetByID(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaCaChieu", id);
            DataTable dt = cn.SqlExcuteQuery("LayCaChieuTheoID", param);
            CaChieuDTO CaChieu = new CaChieuDTO();

            if (dt.Rows.Count != 0)
            {
                CaChieu.MaCaChieu = int.Parse(dt.Rows[0]["MaCaChieu"].ToString());
                CaChieu.GioBD = DateTime.Parse(dt.Rows[0]["GioBD"].ToString());
                CaChieu.GioKT = DateTime.Parse(dt.Rows[0]["GioKT"].ToString());
            }
            return CaChieu;
        }

        public IList GetAll()
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            DataTable dt = cn.SqlExcuteQuery("LayAllCaChieu");

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    CaChieuDTO CaChieu = new CaChieuDTO();
                    CaChieu.MaCaChieu = int.Parse(dr["MaCaChieu"].ToString());
                    CaChieu.GioBD = DateTime.Parse(dr["GioBD"].ToString());
                    CaChieu.GioKT = DateTime.Parse(dr["GioKT"].ToString());
                    array.Add(CaChieu);
                }
            }
            return array;
        }

        public void Insert(CaChieuDTO CaChieu)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@MaCaChieu", CaChieu.MaCaChieu);
            param[1] = new SqlParameter("@GioBD", CaChieu.GioBD);
            param[2] = new SqlParameter("@GioKT", CaChieu.GioKT);
            cn.SqlExcuteNonQuery("InsertCaChieu", param);
        }

        public void Update(CaChieuDTO CaChieu)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@MaCaChieu", CaChieu.MaCaChieu);
            param[1] = new SqlParameter("@GioBD", CaChieu.GioBD);
            param[2] = new SqlParameter("@GioKT", CaChieu.GioKT);
            cn.SqlExcuteNonQuery("UpdateCaChieu", param);
        }

        public void Delete(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaCaChieu", id);
            cn.SqlExcuteNonQuery("DeleteCaChieu", param);
        }
    }
}
